//
//  BKMatchingScoreViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/30.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKMatchingScoreViewController.h"
#import "BKSearchField.h"
#import "BKTextField.h"
#import "BKSearchPickViewDataSource.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import <UIBarButtonItem+RACCommandSupport.h>
#import "BKMatchingScoreViewModel.h"
#import "BKResultOfMatchingScoreViewController.h"
#import <MBProgressHUD.h>

@interface BKMatchingScoreViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet BKSearchField *kaoquTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *kaofenTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *paimingTextField1;
@property (weak, nonatomic) IBOutlet BKTextField *paimingTextField2;
@property (weak, nonatomic) IBOutlet BKSearchField *wenliTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *piciTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *diquTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *zhuanyeTextField;

@property (nonatomic ,strong) BKSearchPickViewDataSource *pickerViewDataSource;
@property (nonatomic ,strong) BKMatchingScoreViewModel *viewModel;


@end

@implementation BKMatchingScoreViewController


- (void)dealloc{

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _diquTextField.dataArray = @[
                                 @"不限",
                                 @"北京",
                                 @"天津",
                                 @"上海",
                                 @"重庆",
                                 @"河北",
                                 @"河南",
                                 @"山东",
                                 @"山西",
                                 @"安徽",
                                 @"江西",
                                 @"江苏",
                                 @"浙江",
                                 @"湖北",
                                 @"湖南",
                                 @"广东",
                                 @"广西",
                                 @"云南",
                                 @"贵州",
                                 @"四川",
                                 @"陕西",
                                 @"青海",
                                 @"宁夏",
                                 @"黑龙江",
                                 @"吉林",
                                 @"辽宁",
                                 @"西藏",
                                 @"新疆",
                                 @"内蒙古",
                                 @"海南",
                                 @"福建",
                                 @"甘肃"];
    _diquTextField.flag = 8011;
    // Do any additional setup after loading the view from its nib.
    _viewModel = [[BKMatchingScoreViewModel alloc] init];
    
     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"kf"]) {
        _kaofenTextField.text = [userDefaults objectForKey:@"kf"];
        
    }
    
    if ([userDefaults objectForKey:@"kq"]) {
        _kaoquTextField.text = [userDefaults objectForKey:@"kq"];
        
    }
    
    if ([userDefaults objectForKey:@"dq"]) {
        _diquTextField.text = [userDefaults objectForKey:@"dq"];
        
    }
    
    if ([userDefaults objectForKey:@"pc"]) {
        _piciTextField.text = [userDefaults objectForKey:@"pc"];
        
    }
    
    _piciTextField.dataArray = @[@"提前批",
                                 @"本科一批",
                                 @"本科二批",
                                 @"本科三批",
                                 @"高职专科"];
    _piciTextField.flag = 8011;
    
    
    _wenliTextField.dataArray = @[@"文科",@"理科"];
    _wenliTextField.flag = 8011;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)submit:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    if ([_kaoquTextField.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择考区" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_kaofenTextField.text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入考分" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;

    }
    
    if( [_wenliTextField.text length] == 0 ){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入文理科" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if( [_piciTextField.text length] == 0 ){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入批次" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_kaoquTextField.text forKey:@"kq"];
    [userDefaults setObject:_kaofenTextField.text forKey:@"kf"];
    if ([_diquTextField.text length]>0) {
        [userDefaults setObject:_diquTextField.text forKey:@"dq"];
    }
    
    if ([_piciTextField.text length]>0) {
        [userDefaults setObject:_piciTextField.text forKey:@"pc"];
    }
    [userDefaults synchronize];
    
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [hud show:YES];
    button.enabled = NO;
    [_viewModel colleageWithkq:_kaoquTextField.text
                            kf:_kaofenTextField.text
                           pwh:_paimingTextField1.text
                           pwe:_paimingTextField2.text
                            wl:_wenliTextField.text
                            pc:_piciTextField.text
                            dp:_diquTextField.text
                            fw:_zhuanyeTextField.text
                   currentPage:@"1"
                       success:^(NSArray *dataArray) {
                           
                           dispatch_queue_t mainQueue = dispatch_get_main_queue();
                           dispatch_async(mainQueue, ^(void) {
                               [hud hide:YES];
                               button.enabled = YES;
                               BKResultOfMatchingScoreViewController *resultMatchingScoreVC = [[BKResultOfMatchingScoreViewController alloc] initWithNibName:NSStringFromClass([BKResultOfMatchingScoreViewController class]) bundle:nil];
                               resultMatchingScoreVC.params = @{@"kq":_kaoquTextField.text,
                                                                @"kf":_kaofenTextField.text
                                                               };
                               resultMatchingScoreVC.dataSource.dataArray = dataArray;
                               
                               [self.navigationController pushViewController:resultMatchingScoreVC animated:YES];
                           
                           });
                          
                           
        
    }
                       failure:^(NSString *message) {
                           
                           
                           dispatch_queue_t mainQueue = dispatch_get_main_queue();
                           dispatch_async(mainQueue, ^(void) {
                               [hud hide:YES];
                               button.enabled = YES;
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                               [alertView show];

                           });
                           
    }];
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
